home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14565 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: news.ov.com!news
  2. From: glenn@ov.com (Fletcher.Glenn@ov.com)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: char* still alive after free ???
  5. Date: 15 Apr 1996 20:24:04 GMT
  6. Organization: OpenVision
  7. Message-ID: <4kub94$5nm@spanky.pls.ov.com>
  8. References: <317269EA.11BB93C2@studbox.uni-stuttgart.de>
  9. Reply-To: glenn@ov.com
  10. NNTP-Posting-Host: foghorn.pls.ov.com
  11.  
  12. In article 11BB93C2@studbox.uni-stuttgart.de, Markus Heller <Markus.Heller@studbox.uni-stuttgart.de> writes:
  13. >Hi,
  14. >
  15. >I have a global variable :
  16. >
  17. >char *text=NULL;
  18. >
  19. >I want to use it to store different "strings" (at diffreent times).
  20. >E.g., if I want to sore 10 characters in text, I do a 
  21. >text=(char *) malloc(10*sizeof(char));
  22. >When I want to use text to store 3 other characters, I first do a
  23. >free(text); text=NULL; and finally a
  24. >text=(char *) malloc(3*sizeof(char));
  25. >But to my surprise there are still the 4thh to 10th charcter of
  26. >text contained before the free(text)/malloc... ???
  27. >This happens under linux, but why ?
  28. >(what I want to to is to get filenames from the user and then open those
  29. > files by a function to which I pass the file name..)
  30. >
  31. >Markus
  32.  
  33.  
  34. 1.  There is no requirement for the memory allocated by malloc() to
  35.     be initialized.  The user is responsible for initialization.
  36.  
  37. 2.  malloc() may re-use memory made available by free().
  38.  
  39. 3.  You were violating the terms of malloc by looking past the 
  40.     third char.  You could have caused some form of memory protection
  41.     error.  BTW, if you were going to store strings, don't forget
  42.     to make room for the trailing null character in your memory allocation
  43.     requests.
  44.  
  45.             Fletcher.Glenn@ov.com
  46.  
  47.